package dbs;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.CallableStatement;
import com.mysql.jdbc.Connection;

/**
 * MySQL 存储过程调用
 * 
 * @author Beanstao@gmail.com
 * @date 2013-10-12上午10:26:38
 * @version 1.0
 */
public class MySQLProcedure
{

	public static void main(String[] args)
	{
		call();

		DBCommon.displayDbProperties(_Connection);

		DBCommon.close(_Connection, _Statement);
	}

	private static CallableStatement _Statement;

	private static void call()
	{
		try
		{
			_Statement = (CallableStatement) _Connection.prepareCall("{call getresult()}");
			// _Statement.registerOutParameter(1, Types.INTEGER);
			boolean hasresult = _Statement.execute();
			// int i = _Statement.getInt(1);
			while (hasresult)
			{
				ResultSet set = _Statement.getResultSet();
				while (set != null && set.next())
				{
					System.out.println("id = " + set.getInt(1) + "; username = " + set.getString(2));
				}
				hasresult = _Statement.getMoreResults();
			}
		}
		catch (Exception e)
		{
			System.out.println("hahad = " + e.toString());
		}
	}

	// ==连接数据库使用的属性
	private static Connection _Connection;
	private static String url = "jdbc:mysql://127.0.0.1:3306/test";
	private static String userName = "root";
	private static String password = "root";

	/*
	 * 数据库连接
	 */
	static
	{
		try
		{
			DriverManager.registerDriver(new com.mysql.jdbc.Driver());
			_Connection = (Connection) DriverManager.getConnection(url, userName, password);
		}
		catch (SQLException sw)
		{
		}
	}

}
